package com.alili_tv.system.service.impl;

import com.alili_tv.system.common.BaseContext;
import com.alili_tv.system.entity.Comment;
import com.alili_tv.system.entity.Likes;
import com.alili_tv.system.mapper.LikesMapper;
import com.alili_tv.system.service.LikesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 点赞表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2023-03-17
 */
@Service
public class LikesServiceImpl extends ServiceImpl<LikesMapper, Likes> implements LikesService {

    @Resource
    private LikesMapper likesMapper;

    @Override
    public boolean isLikeOfVideo(Long userId, Long videoId) {
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Likes::getType, 1)
                .eq(Likes::getCvId, videoId)
                .eq(Likes::getCreateUser, userId);
        long one = this.count(queryWrapper);

        return one != 0;
    }

    @Override
    public long countByVideoId(Long id) {
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Likes::getType, 1)
                .eq(Likes::getCvId, id);
        return this.count(queryWrapper);
    }

    @Override
    public boolean isLikeOfComment(Long userId, Long commentId) {
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Likes::getType, 0)
                .eq(Likes::getCvId, commentId)
                .eq(Likes::getCreateUser, userId);
        long count = this.count(queryWrapper);
        return count != 0;
    }

    @Override
    public void removeByCommentIds(Collection<Long> commentIds) {
        if (commentIds == null || commentIds.size() == 0) {
            return;
        }

        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<Likes>()
                .eq(Likes::getType, 0)
                .in(Likes::getCvId, commentIds);
        this.remove(queryWrapper);
    }

    @Override
    public List<Long> isLikesToCommentIds(long userId, Collection<Long> commentIds) {
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(Likes::getCvId)
                .eq(Likes::getType, 0)
                .in(Likes::getCvId, commentIds)
                .eq(Likes::getCreateUser, userId);
        List<Likes> list = this.list(queryWrapper);
        return list.stream().map(Likes::getCvId).toList();
    }

    @Override
    public void setIsLikesToComments(Collection<Comment> comments) {

        // 获取登录用户id，用于判断用户是否对评论进行了点赞
        Long currentId = BaseContext.getCurrentId();
        if (currentId != null && !comments.isEmpty()) {
            Set<Long> commentIds = new HashSet<>();
            comments.forEach(item -> {
                // 收集一级评论的id
                commentIds.add(item.getId());
                if (item.getSonComments() != null && !item.getSonComments().isEmpty())
                    // 收集二级以下评论的id
                    commentIds.add(item.getSonComments().get(0).getId());
            });
            // 查询已经点赞了的评论id
            List<Long> isLikes = this.isLikesToCommentIds(currentId, commentIds);

            // 设置是否点赞
            comments.forEach(item -> {
                item.setIsLike(isLikes.contains(item.getId()));
                if (item.getSonComments() != null && !item.getSonComments().isEmpty()) {
                    Comment comment = item.getSonComments().get(0);
                    comment.setIsLike(isLikes.contains(comment.getId()));
                }
            });
        }
    }
}
